package app.server.project.main.service.db.entity.user;

import app.server.project.core.syndb.SynDBMgr;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "user_active")
@TableName("user_active")
@Comment(value = "用户活跃度表")
public class UserActiveEntity {

    @Id
    @Column(name = "user_id")
    @Comment("用户主键")
    @TableId("user_id")
    private long userId;

    @TableField("last_login_time")
    @Comment("最后登录时间")
    @Column(name = "last_login_time")
    private LocalDateTime lastLoginTime;

    @TableField("last_offline_time")
    @Comment("最后离线时间时间")
    @Column(name = "last_offline_time")
    private LocalDateTime lastOfflineTime;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public static UserActiveEntity of(long userId) {
        var ret = new UserActiveEntity();
        ret.setUserId(userId);
        ret.setSynDb(true);
        return ret;
    }

    public void setLastLoginTime(LocalDateTime lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserActiveEntity::getLastLoginTime, this);
    }

    public void setLastOfflineTime(LocalDateTime lastOfflineTime) {
        this.lastOfflineTime = lastOfflineTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserActiveEntity::getLastOfflineTime, this);
    }
}
